
<head>
  <title>UCD APP V1.0
</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
  <script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAwbkbZLyhsmTCWXbTcjbgbRSzHs7K5SvaUdm8ua-Xxy_-2dYwMxQMhnagaawTo7L1FE1-amhuQxIlXw"></script>
  <script type="text/javascript">
 <style type="text/css">@import "index.css";</style>

  var ge;
  var la;
  var latitude;
  var longitude;
  var heading;
  var buildings;
  var turning;
  var frameendEL;
  var kmzs;
  var nls;
  var geocoder;
  var currentKmlObject = null;
  google.load("earth", "1");
  google.load("maps", "2.99");
  function init() {
    geocoder = new GClientGeocoder();
    google.earth.createInstance('map3d', initCB, failureCB);
  
  }
  
 function initCB(instance) {
    ge = instance;
    ge.getWindow().setVisibility(true);
  
    frameendEL = false;
    buildings = false;
  
   
    toggleBuildings();
    setInterval('fetchKmlFromInput()',3000);
    ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);
    
    ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
   // ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);
    ge.getLayerRoot().enableLayerById(ge.LAYER_TERRAIN,true);
     create3dModel();
    la = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
    document.getElementById('installed-plugin-version').innerHTML =
    ge.getPluginVersion().toString();
    var win = ge.getWindow();
   
    updateOptions();

    

  }
function fetchKmlFromInput() {
         if (currentKmlObject) {
                ge.getFeatures().removeChild(currentKmlObject);
                currentKmlObject = null;
            }

            var kmlUrlBox = document.getElementById('kml-url');
       var kmlUrl;
       var number=Math.random()*1000;
      
       if(number<200){
       kmlUrl = 'https://leiucd.googlecode.com/svn/trunk/CSIdarkG.kml';
       google.earth.fetchKml(ge, kmlUrl, finishFetchKml);}
       else if(200<number&&number<400){ kmlUrl = 'https://leiucd.googlecode.com/svn/trunk/CSI.kml';
       google.earth.fetchKml(ge, kmlUrl, finishFetchKml);}
       else if(400<number&&number<600){ kmlUrl = 'https://leiucd.googlecode.com/svn/trunk/CSIorange.kml';
       google.earth.fetchKml(ge, kmlUrl, finishFetchKml);}
       else if(600<number&&number<800){ kmlUrl = 'https://leiucd.googlecode.com/svn/trunk/CSIred.kml';
       google.earth.fetchKml(ge, kmlUrl, finishFetchKml);}
       else{ kmlUrl = 'https://leiucd.googlecode.com/svn/trunk/CSIdarkR.kml';
       google.earth.fetchKml(ge, kmlUrl, finishFetchKml);}



      
}


function finishFetchKml(kmlObject) {
                      if (kmlObject) {
                      currentKmlObject = kmlObject;
              ge.getFeatures().appendChild(currentKmlObject);
            } else {
                alert('kml Exception');
            }
        }

  function toggleBuildings(){
  if (!buildings)
  {
  buildings = true;
  //toggleVisibilityNLs(false);
  ge.getLayerRoot().enableLayerById(ge.LAYER_BUILDINGS, buildings);

  }
  else
  {
  buildings = false;
  toggleVisibilityNLs(true);
  ge.getLayerRoot().enableLayerById(ge.LAYER_BUILDINGS, buildings);
  }
  }
  function loadKmz(kmzFile){
  
  var networkLink = ge.createNetworkLink("");
  networkLink.setName("Opened KMZ File");
  networkLink.setFlyToView(false);  
  var link = ge.createLink("");
  link.setHref(kmzFile);
  networkLink.setLink(link);
  networkLink.setVisibility(true);
  ge.getFeatures().appendChild(networkLink);
  nls.push(networkLink);



return networkLink;
  }
  function toggleVisibilityNLs(visibility){
  var nl;
  for (var x=0; x<6; x+=1){
  
  nls[x].setVisibility(visibility);
  
  }
  
  }

  function failureCB() { 

  }
  function createPlacemark(lat,lng){
  var placemark = ge.createPlacemark('');
  var point = ge.createPoint('');
  point.setLatitude(lat);
  point.setLongitude(lng);
  placemark.setGeometry(point);
  ge.getFeatures().appendChild(placemark);
  }
  function za(lat,lng,head,range,altitude,tilt){
    turning = false;
    latitude = lat;
    longitude = lng;
    heading = head;
    la.set(lat,lng,0,ge.ALTITUDE_RELATIVE_TO_GROUND,heading,tilt,range);
    if (!frameendEL){
    toggleFrameendEL();
    }
    ge.getOptions().setFlyToSpeed(.25);
    ge.getView().setAbstractView(la);
    turning = false;
 
    }
  
  function toggleFrameendEL(){
  if (!frameendEL){
  window.google.earth.addEventListener(ge, 'frameend', zoomAround);
  frameendEL = true;
   
  }else{
  google.earth.removeEventListener(ge, 'frameend', zoomAround);
  frameendEL = false;
  
  }
  
  }
  function zoomAround(){
              
    latemp = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);

    
 }
  
function submitLocation() {
var address = document.getElementById('address').value;
geocoder.getLatLng(
address,
function(point) {
if (point && ge != null) {
   var la = ge.createLookAt('');
   la.set(point.y, point.x, 100, ge.ALTITUDE_RELATIVE_TO_GROUND,
         0, 0, 4000);
   ge.getView().setAbstractView(la);
}
}
);

}  
function create3dModel() {
//get the current camera location
var lookAt = ge.getView().copyAsLookAt
(ge.ALTITUDE_RELATIVE_TO_GROUND);

// create the model geometry
var model = ge.createModel('');

// set up the model's location
var loc = ge.createLocation('');
loc.setLatitude( 53.3093);
loc.setLongitude( -6.2244);
model.setLocation(loc);

// set up the model's link (must be a COLLADA file).
// this model was created in SketchUp
var link = ge.createLink('');
model.setLink(link);
link.setHref('https://leiucd.googlecode.com/svn/trunk/'+'csi.dae');

// create the model placemark and add it to Earth
var modelPlacemark = ge.createPlacemark('');
modelPlacemark.setGeometry(model);
ge.getFeatures().appendChild(modelPlacemark);

// zoom in on the model
//lookAt.setRange(300);
//lookAt.setTilt(80);
//ge.getView().setAbstractView(lookAt);

// persist the placemark for other interactive samples
window.placemark = modelPlacemark;

} 

 
  function buttonClick(lat,lng,head,range,altitude,tilt) {
    za(lat,lng,head,range,altitude,tilt); 
  }

  function stopFly() {
    if(frameendEL){
      toggleFrameendEL();
    }
    ge.getOptions().setFlyToSpeed(1);
    turning = false;
  }

 function updateOptions() {
  var options = ge.getOptions();
  var form = document.options;
  
  options.setStatusBarVisibility(form.statusbar.checked);
  options.setOverviewMapVisibility(form.overview.checked);
 }
  </script>
</head>
  <body onload='init() 'id='body'>
 <body onload="setInterval('fetchKmlFromInput()',3000)">
    <div style='font-weight:bold; font-size: 16pt; font-family: arial; width: 640px'>
    <center>UCD project</center>
  </div>
    <table width='824' height="635" cellspacing='6'>
      <tr>
      <tr align=left>
        <td valign=top><div id='map3d' style='border: 1px solid silver; height: 550px; width: 780px;'
></div></td>
        <td><br/>
        </td>
      </tr>
      <tr align=left>
        <td valign=top><input name="button" type="button" onClick="buttonClick(53.307632,-6.221766,0,700,0,60)" value="UCD"/>
        <input name="button" type="button" onClick="buttonClick(53.309246, -6.223991,0,100,0,60)" value="CSI"/>
        </td>
      </tr>
    </table>
    <div>
<form name='searchform' id='searchform' action='javascript:submitLocation();void(0);'>
<input type=text size=60 id='address'></input>
<input type=submit value='Go!'>
</form>
</div>

 <div id='options_container'> 
    <form name="options" action='javascript:updateOptions();'>
      <input type="checkbox" onclick='updateOptions()' name="statusbar" />Status Bar
      <br />
      <input type="checkbox" onclick='updateOptions()' name="overview" />Overview Map
      <br />
   </form>
     

  </div>  


</body>
</html>
